package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.common.springmvc.LeaseException;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.RoomInfoMapper;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.j2objc.annotations.AutoreleasePool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private LeaseAgreementService leaseAgreementService;

    @Override
    public void customPage(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.customSelectPage(page,queryVo);

    }
    private void removeRoomRelatedData(Long roomId) {
        //删除房间属性
        LambdaQueryWrapper<RoomAttrValue>roomAttrValueLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, roomId);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
        //删除房间配套
        LambdaQueryWrapper<RoomFacility>roomFacilityLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId, roomId);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
        //删除房间标签
        LambdaQueryWrapper<RoomLabel>roomLabelLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, roomId);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);
        //删除支付方式
        LambdaQueryWrapper<RoomPaymentType>roomPaymentTypeLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, roomId);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
        //删除房间租期
        LambdaQueryWrapper<RoomLeaseTerm>roomLeaseTermLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomId);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        //删除图片
        LambdaQueryWrapper<GraphInfo>graphInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, roomId);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
    }

    @Override
    public void customSaveOrUpdate(RoomSubmitVo roomSubmitVo) {
        //判断保存还是更新
        boolean isUpdated = roomSubmitVo.getId() != null;
        //调用保存或者更新

        saveOrUpdate(roomSubmitVo);
        //如果是更新
        if (isUpdated) {
            removeRoomRelatedData(roomSubmitVo.getId());
        }
        //重新插入中间表的关联数据
        //保存房间属性
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)){
            List<RoomAttrValue>roomAttrValueList=new ArrayList<>(attrValueIds.size());
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue=RoomAttrValue.builder()
                        .attrValueId(attrValueId).roomId(roomSubmitVo.getId()).build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }
        //保存房间配套
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            List<RoomFacility>roomFacilityList=new ArrayList<>(facilityInfoIds.size());
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility=RoomFacility.builder()
                        .facilityId(facilityInfoId).roomId(roomSubmitVo.getId()).build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }
        //保存房间标签
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)){
            List<RoomLabel>roomLabelList=new ArrayList<>(labelInfoIds.size());
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel=RoomLabel.builder()
                        .labelId(labelInfoId).roomId(roomSubmitVo.getId()).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }
        //保存支付方式
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)){
            List<RoomPaymentType>roomPaymentTypeList=new ArrayList<>(paymentTypeIds.size());
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType=RoomPaymentType.builder().paymentTypeId(paymentTypeId)
                        .roomId(roomSubmitVo.getId()).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }
        //保存租期
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)){
            List<RoomLeaseTerm>roomLeaseTermList=new ArrayList<>(leaseTermIds.size());
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm=RoomLeaseTerm.builder().leaseTermId(leaseTermId)
                        .roomId(roomSubmitVo.getId()).build();
                roomLeaseTermList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }
        //保存图片
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)) {
            List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
    }


    @Override
    public RoomDetailVo customGetById(Long id) {
        return roomInfoMapper.customSelectById(id);
    }

    @Override
    public void customRemoveById(Long id) {
        //判断是否存在租约
        LambdaQueryWrapper<LeaseAgreement>leaseAgreementLambdaQueryWrapper=new LambdaQueryWrapper<>();
        leaseAgreementLambdaQueryWrapper.eq(LeaseAgreement::getRoomId,id);
        List<LeaseStatus> list =new ArrayList<>();
        list.add(LeaseStatus.SIGNED);
        list.add(LeaseStatus.WITHDRAWING);
        list.add(LeaseStatus.RENEWING);
        leaseAgreementLambdaQueryWrapper.in(LeaseAgreement::getStatus,list);
        long count = leaseAgreementService.count(leaseAgreementLambdaQueryWrapper);
        if (count>0){
            throw  new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        //删除中间表
        removeRoomRelatedData(id);
        //删除房间表
        removeById(id);

    }
}




